/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*******************************************************************************
 *               Page Navigation Function
 ******************************************************************************/
var pagecount = 0;
var i=0;
var request;

function ajaxPage(pageurl){
    $.ajax({
        type:"GET",
        url: pageurl,
        dataType:"html",
        success:function(data, textStatus, jqXHR){
            var ajaxedPage = $('#ajaxed');
            ajaxedPage.html(data);
            var pageData= ajaxedPage.find("#inner_main").html();
            $('#page'+i).hide();
            i++;
            var pageid = "page"+ i;
            $('#content').append("<div id='"+pageid+"' class='inner_page'></div>");
            $('#'+pageid).css("height","0%");
            $('#content').find("#"+pageid).html(pageData);
           
            $('#'+pageid).animate({
                height:'100%'
            }, {
                duration: 1200,
                specialEasing: {
                    height: 'linear'
                }
            });
            ajaxedPage.html("");
            $('input').each(function(){
                if($(this).attr("type")=="submit" || $(this).attr("type")=="Submit"){
                    $(this).addClass("btn btn-primary");
                }
                else if($(this).attr("type")=="button"){
                    $(this).addClass("btn");
                }
                else if($(this).attr("type")=="reset"){
                    $(this).addClass("btn");
                }
            });

            $('#content').find('a').click(function(e){
                e.preventDefault();
                if($(this).attr("href")!="#"){
                    ajaxPage($(this).attr("href"));
                }
            });

            setupSubmit(pageid);
        }
    });
    /*$.ajax({
        url: pageurl,
        cache: false,
        dataType: "html"
    }).done(function(responseHTML) {
        
    });*/
    return false;
}

function setupSubmit(divele){
    $("#"+divele+" form").each(function (){
        $(this).submit(function(event){
            // abort any pending request
            if (request) {
                request.abort();
            }

            // setup some local variables
            var $form = $(this);
            // let's select and cache all the fields
            var $inputs = $form.find("input, select, button, textarea");
            // serialize the data in the form
            var serializedData = $form.serialize();

            var actionurl = $form.attr('action');

            // let's disable the inputs for the duration of the ajax request
            $inputs.prop("disabled", true);

            // fire off the request to /form.php
            request = $.ajax({
                url: actionurl,
                type: "POST",
                data: serializedData,
                // callback handler that will be called on success
                success:function (response, textStatus, jqXHR){
                    var ajaxedPage = $('#ajaxed');
                    ajaxedPage.html(response);
                    var pageData= ajaxedPage.find("#inner_main").html();
                    $('#page'+i).hide();
                    i++;
                    var pageid = "page"+ i;
                    $('#content').append("<div id='"+pageid+"' class='inner_page'></div>");
                    $('#'+pageid).css("height","0%");
                    $('#content').find("#"+pageid).html(pageData);

                    $('#'+pageid).animate({
                        height:'100%'
                    }, {
                        duration: 1200,
                        specialEasing: {
                            height: 'linear'
                        }
                    });
                    ajaxedPage.html("");
                    setupSubmit(pageid);
                },
                // callback handler that will be called on failure
                error:function (jqXHR, textStatus, errorThrown){
                    // log the error to the console
                    console.error(
                        "The following error occured: "+
                        textStatus, errorThrown
                        );
                }
            });

            // callback handler that will be called regardless
            // if the request failed or succeeded
            request.always(function () {
                // reenable the inputs
                $inputs.prop("disabled", false);
            });

            // prevent default posting of form
            event.preventDefault();
            return false;
        });
        return false;
    });
    return false;
}

function validationAdd(){
    $("form").validate({
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            name: "Please specify your name",
            email: {
                required: "We need your email address to contact you",
                email: "Your email address must be in the format of name@domain.com"
            }
        }
    });

}

function checkdate(){
    var validformat=/^\d{4}\-\d{2}\-\d{2}$/ ;

}
